var id = 0;
var total_duration = 0;
var railsTime = 0;

function extract_params( name )
{
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
	return "";
	else
	return results[1];
}


function addParams(obj,key,value){
	obj[key] = value;
	return obj;
}

function addDateParametersToPostArray(postParams){
	postParams = addParams(postParams,'start',extract_params( 'start'));
	postParams['start'] = extract_params( 'start');
	if (extract_params( 'range') != "")
	postParams = addParams(postParams,'range',extract_params( 'range'));
	else if (extract_params( 'end') != "")
	postParams = addParams(postParams,'end',extract_params( 'end'));
	return postParams;
}



function getAwayEntryId(parentRow){
	var id = parentRow.children('.checkbox').eq(0).find("input").val();	
	return id;
}

function getAwayEntryRailsTime(parentRow){
	var railsTime = parentRow.children('.name.away').eq(0).find("input").val();	
	return railsTime;
}



function splitAwayEntry(obj){
	var parentRow = $(obj).closest('tr');
	id = getManualActivityId(parentRow);
	$.get('summaries/get_away_entry_info', { entry_id: id}, function(data) {
		awayEntry = jQuery.parseJSON(data);	
		total_duration = 	awayEntry.finish - awayEntry.start	
		$("#away_entry_total_2").text("Total duration: " + displayDuration(total_duration));
		$("#seg_1_duration").text(formatSliderDuration(total_duration/2,1));
		$("#seg_2_duration").text(formatSliderDuration(total_duration/2,1));
		$('#split_away_modal').jqmShow();
	});
}




function annotateAwayEntry(obj){
var parentRow = $(obj).closest('tr');
	id = getManualActivityId(parentRow);
	railsTime = getAwayEntryRailsTime(parentRow);
	
	$.get('summaries/get_away_entry_info', { entry_id: id}, function(data) {
		var entry = jQuery.parseJSON(data);
		
		var duration = entry.finish - entry.start;
		
		$(".away_time").html(entry.away_time);
		$(".away_duration").html(entry.away_duration);
		
		$("#away_description").val(entry.desc);
		$('#annotate_away_modal').jqmShow();
	});
}

function updateAwayEntryinUI(desc){
	$("#"+id).children("span").eq(0).text(desc + " (" + railsTime+")");
	var img = $("#"+id).closest("tr").children('.icon').eq(0).find("img")
	img.attr("src","/images/famfamfam/accept.png");
	img.attr("alt","Accept");
}


function updateSplitAwayEntryinUI(){
	$("p").insertAfter("#id");
	$("p").insertAfter("#id");
	//remove
	//all 2 lines


}

function percentTotal(awayEntry){
	return "12%";
	
}


function awayRowHtml(awayEntry){
	var duration = awayEntry.finish - awayEntry.start;
	var html = "";
	html += "<tr style=\"\" id=\"away_summary_"+awayEntry.id+"\" class=\"summary-card child away_2010-12-08\">";
	html += "<td class=\"checkbox\"><input type=\"checkbox\" value=\""+awayEntry.id+"\" name=\"away_summary[]\" id=\"away_summary_\"></td>";
	html += "<td class=\"icon\"><img src=\"/images/famfamfam/accept.png?1291663020\" alt=\"Accept\"></td>";
	html += "<td id=\""+awayEntry.id+"\" class=\"name away\"><span>"+awayEntry.description+"</span>";
	html += "<div class=\"away-buttons\" style=\"display: block;\">";
	html += "<div class=\"inline-button split_away_button\">";
	html += "Split";
	html += "</div>";
	html += "<div class=\"inline-button annotate_away_button\">";
	html += "Annotate";
	html += "</div>";
	html += "</div>";
	html += "</td>";
	html += "<td seconds=\""+duration +"\" class=\""+displayDuration(duration)+"\">36m 18s</td>";
	html += "<td class=\"percent-total\"><div style=\"width: "+percentTotal(awayEntry)+";\" class=\"percent-total-chart\"></div></td>";
	html += "</tr>";
	return html;
}



function updateSplitAwayEntryinDB(){
	desc = $("#away_description").val();
	split_away_slider_val = $("#split_away_slider").slider("value");
	$("#split_away_slider").slider("value", 50);
	var postParams = {};
	postParams = addDateParametersToPostArray(postParams);
	postParams = addParams(postParams,'split_away_slider_val',split_away_slider_val);
	postParams = addParams(postParams,'split_away_id',id);
	$.post('/summaries/ajax_split_away',postParams, null, 'script');	
	$("#split_away_form")[0].reset();
	$(":input").blur();
}



function updateAwayEntryinDB(){
	desc = $("#away_description").val();
	
	updateAwayEntryinUI(desc);

	var postParams = {};
	postParams = addDateParametersToPostArray(postParams);
	postParams = addParams(postParams,'description',desc);
	postParams = addParams(postParams,'entry_id',id);
	$.post('/summaries/ajax_annotate_away',postParams, null, 'script');	
}
